package com.tuanbiao.ui.blast;

import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;

import com.hzjq.lib.cap.bean.ErrorCap;
import com.tuanbiao.R;
import com.tuanbiao.base.mvp.BasePresenterActivity;
import com.tuanbiao.controllers.BlastController;
import com.tuanbiao.databinding.ActivityBlastCheckBinding;
import com.tuanbiao.dialog.BaseDialog;
import com.tuanbiao.dialog.ErrorDialog;
import com.tuanbiao.dialog.MessageDialog;
import com.tuanbiao.dialog.WaitDialog;
import com.tuanbiao.events.BlastErrorEvent;
import com.tuanbiao.keyevent.KeyEventHelper;
import com.tuanbiao.keyevent.listener.OnBlastNettedKeyDownListener;
import com.tuanbiao.models.BlastModel;
import com.tuanbiao.presenter.BlastNettedPresenter;
import com.tuanbiao.utils.ClickUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import timber.log.Timber;

public class BlastNettedActivity extends BasePresenterActivity<BlastNettedPresenter, ActivityBlastCheckBinding> implements BlastController.CheckView {
    private WaitDialog.Builder waitDialogBuilder;
    private WaitDialog.Builder checkDialog;
    public static void start(Context context) {
        Intent starter = new Intent(context, BlastNettedActivity.class);
        context.startActivity(starter);
    }

    private BaseDialog backDialog;

    @Override
    protected ActivityBlastCheckBinding getBindingView() {
        return ActivityBlastCheckBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        binding.btnRecheck.setEnabled(false);
        binding.btnCancelCheck.setEnabled(false);
        binding.ivToolbarBack.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            showExitDialog();
        });
        binding.btnCancelCheck.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            showExitDialog();
        });
        binding.btnStartCharge.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            mPresenter.startCharge();
        });
        binding.btnRecheck.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            mPresenter.checkNetted();
        });
    }

    @Override
    public void onBackPressed() {
        showExitDialog();
    }

    private void showExitDialog() {
        backDialog = new MessageDialog.Builder(getActivity())
                .setTitle("提示")
                .setMessage("是否终止当前操作并退出！")
                .setListener(dialog -> {
                    //mPresenter.cancelNetted();
                    mPresenter.exitNetted();
                }).show();
    }

//    private void showCancelDialog() {
//        new MessageDialog.Builder(getActivity())
//                .setTitle("提示")
//                .setMessage("是否取消当前操作！")
//                .setListener(dialog -> {
//                    showExitDialog();
//                    mPresenter.cancelNetted();
//                        killMyself();
//                }).show();
//    }

    @Override
    protected BlastNettedPresenter createPresenter() {
        return new BlastNettedPresenter(new BlastModel(), this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Timber.e("onKeyDownx keyCode:"+keyCode);
        KeyEventHelper.getInstance().checkBlastNettedKeyDown(keyCode, new OnBlastNettedKeyDownListener() {
            @Override
            public void onCharge() {
                mPresenter.startCharge();
            }

            @Override
            public void onCheckErrorCaps() {
                mPresenter.checkErrorCaps();
            }
        });
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void showCheckDialog() {
        if (null == checkDialog)
            checkDialog = new WaitDialog.Builder(getActivity()).setMessage("正在检测中");
        checkDialog.show();
    }

    @Override
    public void hideCheckDialog() {
        if (null != checkDialog && checkDialog.isShowing()) {
            checkDialog.dismiss();
        }
    }


    @Override
    public void showProgress(int progress, String msg) {
//        binding.wlvCheck.setTopTitle(msg);
        binding.wlvCheck.setProgressValue(progress);
        binding.wlvCheck.setCenterTitle(progress + "%");
    }

    @Override
    public void showVoltageElectric(float voltage, float electric) {
        binding.tvVoltage.setText(getString(R.string.blast_voltage, voltage));
        binding.tvCurrent.setText(getString(R.string.blast_current, electric));
    }

    @Override
    public void showCheckStatus(String status) {
        binding.tvCheckStatus.setText(status);
    }

    @Override
    public void showCheckNote(String msg) {
        binding.tvCheckNote.setText(msg);
    }

    @Override
    public void showCheckResult(boolean visibility, String msg) {
        binding.tvCheckResult.setVisibility(visibility ? View.VISIBLE : View.GONE);
        binding.tvCheckNote.setVisibility(visibility ? View.GONE : View.VISIBLE);
        binding.tvCheckResult.setText(msg);
    }

    @Override
    public void showCheckErrorResult(String msg) {
        if(backDialog != null && backDialog.isShowing()){
            backDialog.dismiss();
        }
        ErrorDialog.show(getActivity(), msg, false,new ErrorDialog.OnErrorMenuItemClickListener() {
            @Override
            public void onLookErrorListClick() {
                mPresenter.checkErrorCaps();
            }

            @Override
            public void onExitClick() {
                mPresenter.exitNetted();
            }

            @Override
            public void onNextClick() {
                mPresenter.doErrorNext();
            }
        });

    }

    @Override
    public void setChargeEnable(boolean enable) {
        binding.btnStartCharge.setEnabled(enable);
    }

    @Override
    public void setRecheckEnable(boolean enable) {
        binding.btnRecheck.setEnabled(enable);
    }

    @Override
    public void setCancelEnable(boolean enable) {
        binding.btnCancelCheck.setEnabled(enable);
    }

    @Override
    public void startCharge() {
        BlastActivity.start(getActivity());
        finish();
    }

    @Override
    public void startErrorCaps(List<ErrorCap> errorCaps) {
        EventBus.getDefault().postSticky(new BlastErrorEvent(errorCaps));
        BlastCheckErrorActivity.start(getActivity());
    }

    @Override
    public void showExit() {
        if (null == waitDialogBuilder) {
            waitDialogBuilder = new WaitDialog.Builder(getActivity()).setMessage("正在退出");
        }
        waitDialogBuilder.show();
    }

    @Override
    public void hideExit() {
        if (null != waitDialogBuilder && waitDialogBuilder.isShowing()) {
            waitDialogBuilder.dismiss();
        }
    }

    @Override
    public void exitFailed() {
        binding.btnRecheck.setEnabled(false);
        binding.btnCancelCheck.setEnabled(true);
    }

    @Override
    protected void showShortCircuit(boolean isClose) {
        if (isClose) {
            mPresenter.cancelNetted();
            mPresenter.exitNetted();
        }
    }
}